package com.spider.silence.pool;

import com.google.common.collect.Lists;
import com.spider.silence.downloader.ZuiChengduDownLoader;
import com.spider.silence.utils.PropertiesUtil;
import org.apache.log4j.Logger;

import java.lang.reflect.InvocationTargetException;
import java.util.LinkedList;

/**
 * Created by 夏茂轩 on 2016/12/26.
 */
public class ZuiChengduDownLoaderPool{
    private Logger logger = Logger.getLogger(ZuiChengduDownLoaderPool.class);
    private static  ZuiChengduDownLoaderPool instance=new ZuiChengduDownLoaderPool();;
    protected  LinkedList<ZuiChengduDownLoader> pool = Lists.newLinkedList();

    /**
     * @author 夏茂轩
     * @date 2017/1/15 22:26
     * @description 初始化最成都下载池
     */
    private   ZuiChengduDownLoaderPool(){
        int initNum= Integer.parseInt(PropertiesUtil.getVal("ZuiChengDu.DownLoader.Pool.initNum"));
        ZuiChengduDownLoader downLoader =new ZuiChengduDownLoader();
        downLoader.init();
        for(int i=0;i<initNum;i++){
            ZuiChengduDownLoader zuiChengduDownLoader = null;
            try {
                synchronized (pool){
                    zuiChengduDownLoader =  downLoader.cloneDownLoader();
                    pool.add(zuiChengduDownLoader);
                    pool.notifyAll();
                }
            }  catch (NoSuchMethodException e) {
                e.printStackTrace();
                logger.error(e);
            } catch (InstantiationException e) {
                e.printStackTrace();
                logger.error(e);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
                logger.error(e);
            } catch (InvocationTargetException e) {
                logger.error(e);
                e.printStackTrace();
            }
        }
    }

    /**
     * @author 夏茂轩
     * @date 2017/1/15 22:26
     * @description 获取最成都下载池实例
     * @return ZuiChengduDownLoaderPool
     */
    public  static ZuiChengduDownLoaderPool getInstance(){
        return  instance;
    }
    public  void releaseDownLoader(ZuiChengduDownLoader downloader) {
        if (downloader != null) {
            synchronized (pool) {
                pool.addLast(downloader);
                pool.notifyAll();
            }
        }
    }

    /**
     * @author 夏茂轩
     * @date 2017/1/15 22:26
     * @description 在指定时间内获取ZuiChengduDownLoader,如果指定时间没有拿到（没有线程返还）,则返回null
     * @param mills 指定时间
     * @return ZuiChengduDownLoader
     * @throws InterruptedException 中断异常
     */
    public  ZuiChengduDownLoader fetchDownLoader(long mills) throws InterruptedException {
        synchronized (pool){
            if(mills<=0){
                while (pool.isEmpty()){
                    pool.wait();
                }
                return pool.removeFirst();
            }else {
                long future= System.currentTimeMillis()+mills;
                long remianing=mills;
                while (pool.isEmpty() && remianing >0){
                    pool.wait(remianing);
                    remianing=future-System.currentTimeMillis();
                }
                ZuiChengduDownLoader downLoader =null;
                if(!pool.isEmpty()){
                    downLoader=pool.removeFirst();
                }
                return downLoader;
            }
        }
    }

    /**
     * @author 夏茂轩
     * @date 2017/1/15 22:27
     * @description 清空池
     */
    public void empty(){
        pool=Lists.newLinkedList();
    }
}
